/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cs.util.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * MySqlDatabaseMetaInfo can be used to get information about the types and properties of the tables in a database.
 * @author dimitris@jmike.gr
 */
public class MySqlDatabaseMetaInfo extends GenericDatabaseMetaInfo implements DatabaseMetaInfo {

    /**
     * Constructs a new MySqlDatabaseMetaInfo.
     * @param db The parent database.
     * @throws java.sql.SQLException
     */
    public MySqlDatabaseMetaInfo(Database db) throws SQLException {
        /* get the resultset */
        PreparedStatement ps = db.getPreparedStatement("SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = ? AND `TABLE_TYPE` = 'BASE TABLE';", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//Create prepared statement
        ps.setString(1, db.getSchemaName());//set schema
        ResultSet rs = ps.executeQuery();
        /* set the table index */
        rs.beforeFirst();//reset rs cursor
        int i = 0;//initialize internal id
        while (rs.next()) {//iterate rs
            index.put(rs.getString("TABLE_NAME"), Integer.valueOf(i));//put column name, internal id to index
            i++;//increase internal id (step 1)
        }
        index.put("information_schema.TABLES", i);//add information_schema.TABLES
        /* close the resultset */
        rs.close();
        ps.close();
        /* get another resultset */
        ps = db.getPreparedStatement("SELECT `CONSTRAINT_NAME`, `TABLE_NAME`, `COLUMN_NAME`, `REFERENCED_TABLE_NAME`, `REFERENCED_COLUMN_NAME` FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `CONSTRAINT_SCHEMA` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL ORDER BY `CONSTRAINT_NAME` ASC, `POSITION_IN_UNIQUE_CONSTRAINT` ASC;", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//Create prepared statement
        ps.setString(1, db.getSchemaName());//set schema
        rs = ps.executeQuery();
        /* set foreignKeys */
        rs.beforeFirst();//reset rs cursor
        i = 0;//set internal id to zero
        while (rs.next()) {//iterate rs
            foreignKeys.put(rs.getString("CONSTRAINT_NAME"), new ArrayList<Integer>());//put 'foreign key name, empty list of internal ids' to index
            i++;//increase internal id (step 1)
        }
        /* set attributes array size */
        fkColumn1 = new String[i];
        fkColumn2 = new String[i];
        rs.beforeFirst();//reset rs cursor
        i = 0;//reset internal id
        while (rs.next()) {//iterate rs again
            fkColumn1[i] = rs.getString("COLUMN_NAME");
            fkColumn2[i] = rs.getString("REFERENCED_COLUMN_NAME");
            final String key = rs.getString("CONSTRAINT_NAME");
            foreignKeys.get(key).add(Integer.valueOf(i));//append internal id to this foreign key
            relations.put(rs.getString("TABLE_NAME") + "-" + rs.getString("REFERENCED_TABLE_NAME"), key);//put 'table1-table2, foreign key name' to the relations
            i++;//increase internal id (step 1)
        }
        /* close the resultset */
        rs.close();
        ps.close();
    }
}

